package yzx.firstletter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import yzx.firstletter.appList.AppListAdapter;
import yzx.firstletter.appList.AppListDecoration;
import yzx.firstletter.appList.AppPinYinUtil;
import yzx.firstletter.appList.fragment.LetterSelectFragment;
import yzx.firstletter.util.AppInstallUtil;
import yzx.firstletter.util.GenericCallback;
import yzx.firstletter.util.ID;
import yzx.firstletter.util.Injector;
import yzx.firstletter.util.LocalTask;

public class MainActivity extends AppCompatActivity {

    @ID(id=R.id.recyclerView)
    private RecyclerView recyclerView;
    @ID(id=R.id.pb_loading)
    private View progressView;

    private AppListAdapter adapter;
    private volatile List<AppInstallUtil.AppInfo> allInstalledAppList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Injector.injectField(this);

        initLetterSelect();
        initList();
    }


    private void initLetterSelect(){
        View container = findViewById(R.id.letter_container);
        container.getLayoutParams().height = getResources().getDisplayMetrics().widthPixels/9*4;
        container.setLayoutParams(container.getLayoutParams());
        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        final LetterSelectFragment letterSelectFragment = new LetterSelectFragment();
        letterSelectFragment.setSelectedState(LetterSelectFragment.TAG_ALL);
        trans.replace(R.id.letter_container, letterSelectFragment);
        letterSelectFragment.onClickCallback = new GenericCallback<String>() {
            public void callback(String tag) {
                if(allInstalledAppList==null||allInstalledAppList.isEmpty() || adapter==null)
                    return ;
                if(progressView.getVisibility()==View.VISIBLE) return ;
                letterSelectFragment.setSelectedState(tag);
                adapter.getList().clear();
                if(tag.equals(LetterSelectFragment.TAG_ALL)){
                    adapter.getList().addAll(allInstalledAppList);
                }else
                    adapter.getList().addAll(AppPinYinUtil.filterListByTag(allInstalledAppList, tag));
                adapter.notifyDataSetChanged();
            }
        };
        trans.commitNow();
    }

    private void initList(){
        showLoading(true);
        LocalTask.exec(new Runnable() {
            public void run() {
                allInstalledAppList = AppInstallUtil.getAllInstalledAppInfo(MainActivity.this, false);
                AppPinYinUtil.sort(allInstalledAppList);
                runOnUiThread(new Runnable() {
                    public void run() {
                        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                        recyclerView.setHasFixedSize(true);
                        recyclerView.addItemDecoration(new AppListDecoration());
                        adapter = new AppListAdapter(new ArrayList<>(allInstalledAppList));
                        recyclerView.setAdapter(adapter);
                        adapter.onItemClickListener = new GenericCallback<AppInstallUtil.AppInfo>() {
                            public void callback(AppInstallUtil.AppInfo appInfo) {
                                openAPP(appInfo);
                            }
                        };
                        showLoading(false);
                    }
                });
            }
        });
    }

    private void openAPP(AppInstallUtil.AppInfo info){
        try {
            Intent intent = getPackageManager().getLaunchIntentForPackage(info.packageName);
            startActivity(intent);
        }catch (Exception e){
            Toast.makeText(this, "该应用无启动界面", Toast.LENGTH_SHORT).show();
        }
    }

    private void showLoading(boolean show){
        recyclerView.setVisibility(show?View.GONE:View.VISIBLE);
        progressView.setVisibility(!show?View.GONE:View.VISIBLE);
        if(!show){
            recyclerView.setAlpha(0f);
            ViewCompat.animate(recyclerView).alpha(1f).setDuration(300).start();
        }
    }

}